import Vue from 'vue'
import Cookies from 'js-cookie'
import VueI18n from 'vue-i18n'
import locale from 'element-ui/lib/locale'
import messages from './langs/index'


Vue.use(VueI18n)
export function getLanguage () {
  const chooseLanguage = Cookies.get('lang')

  if (chooseLanguage) {
    return chooseLanguage
  }
  const language = navigator.language.toLowerCase()
  const locales = Object.keys(messages)

  for (const locale of locales) {
    if (language.includes(locale)) {
      return locale
    }
  }
}
const i18n = new VueI18n({
  locale: getLanguage() || 'zh',
  messages,
  silentTranslationWarn: true
})
locale.i18n((key, value) => i18n.t(key, value)) // 重点：为了实现element插件的多语言切换
export default i18n
